package alibaba.server;

import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ronenkat
 *
 */
public class Listing {
	public static class FileNamePair {
		private String filename;
		private String owner;

		public FileNamePair() {}

		public FileNamePair(String filename, String owner) {
			this.filename = filename;
			this.owner = owner;
		}

		public String getFilename() {
			return filename;
		}

		public void setFilename(String filename) {
			this.filename = filename;
		}

		public String getOwner() {
			return owner;
		}

		public void setOwner(String owner) {
			this.owner = owner;
		}

		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result + ((filename == null) ? 0 : filename.hashCode());
			result = prime * result + ((owner == null) ? 0 : owner.hashCode());
			return result;
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			FileNamePair other = (FileNamePair) obj;
			if (filename == null) {
				if (other.filename != null)
					return false;
			} else if (!filename.equals(other.filename))
				return false;
			if (owner == null) {
				if (other.owner != null)
					return false;
			} else if (!owner.equals(other.owner))
				return false;
			return true;
		}
	}

	/**
	 * @param listOfFiles
	 */
	public Listing(Map<FileNamePair, ListingEntry> listOfFiles) {
		super();
		this.listOfFiles = listOfFiles;
	}

	/**
	 */
	public Listing() {
		super();
		this.listOfFiles = new HashMap<FileNamePair, ListingEntry>();
	}

	private Map<FileNamePair, ListingEntry> listOfFiles;

	/**
	 * @return the listOfFiles
	 */
	public Map<FileNamePair, ListingEntry> getListOfFiles() {
		return listOfFiles;
	}

	/**
	 * @param listOfFiles
	 *            the listOfFiles to set
	 */
	public void setListOfFiles(Map<FileNamePair, ListingEntry> listOfFiles) {
		this.listOfFiles = listOfFiles;
	}

	public ListingEntry getFileEntry(FileNamePair fileNamePair) {
		return this.listOfFiles.get(fileNamePair);
	}

	public ListingEntry getFileEntry(String filename, String owner) {
		return getFileEntry(new FileNamePair(filename, owner));
	}

	public void addFileEntry(ListingEntry entry) {
		this.listOfFiles.put(new FileNamePair(entry.getFilename(), entry.getOwner()), entry);
	}

	/**
	 * Write the backup server meta information repository to disk
	 *
	 * @param filename
	 * @param listing
	 * @throws FileNotFoundException
	 */
	public static void writeListing(String filename, Listing listing) throws FileNotFoundException  {
		XMLEncoder e = new XMLEncoder(new BufferedOutputStream(new FileOutputStream(filename)));
		e.writeObject(listing);
		e.close();
	}

	/**
	 * Read the backup server meta information repository from disk
	 *
	 * @param filename
	 * @return
	 * @throws FileNotFoundException
	 */
	public static Listing readListing(String filename) throws FileNotFoundException {
		XMLDecoder d = new XMLDecoder(new BufferedInputStream(new FileInputStream(filename)));
		Object o = d.readObject();
		if (o instanceof Listing) {
			return (Listing) o;
		}
		return null;
	}
}
